package net.lucamasini.fantacalcio.gwt.client.view;

import static net.lucamasini.fantacalcio.gwt.client.Fantacalcio.getEventBus;
import net.lucamasini.fantacalcio.gwt.client.event.AggiornaSquadraEvent;
import net.lucamasini.fantacalcio.gwt.client.event.CancellaSquadraEvent;
import net.lucamasini.fantacalcio.gwt.client.event.HomeEvent;
import net.lucamasini.fantacalcio.gwt.client.event.NuovaSquadraEvent;
import net.lucamasini.fantacalcio.server.model.Squadra;

import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.IconCellDecorator;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.HasData;

public class GiocatoriPanel extends Composite implements GiocatoriView {
    interface Binder extends UiBinder<Widget, GiocatoriPanel> {
    }

    private static final Binder BINDER = GWT.create(Binder.class);

    public static interface Resources extends ClientBundle {

        ImageResource subbuteo();
      }

    private static Resources DEFAULT_RESOURCES;
    
    private static Resources getDefaultResources() {
        if (DEFAULT_RESOURCES == null) {
          DEFAULT_RESOURCES = GWT.create(Resources.class);
        }
        return DEFAULT_RESOURCES;
    }
    
    
	public GiocatoriPanel() {
        initWidget(BINDER.createAndBindUi(this));
        
        final Column<Squadra, String> casa = new Column<Squadra, String>(
				new EditTextCell()) {
        	{
        		setFieldUpdater(new FieldUpdater<Squadra, String>() {

        			@Override
        			public void update(int index, Squadra object, String value) {

    					object.setUsername(value);	
        			}
        		});
        	}
			@Override
			public String getValue(Squadra object) {
				return object.getUsername();
			}
		};
		
        final Column<Squadra, String> trasferta = new Column<Squadra, String>(
				new EditTextCell()) {
        	{
        		setFieldUpdater(new FieldUpdater<Squadra, String>() {

        			@Override
        			public void update(int index, Squadra object, String value) {

        				object.setNome(value);
        			}
        		});
        	}
			@Override
			public String getValue(Squadra object) {
				return object.getNome();
			}
		};

		giocatoriCellTable.addColumn(new Column<Squadra, String>(
			new IconCellDecorator<String>(getDefaultResources().subbuteo(), new TextCell())) {
				@Override
				public String getValue(Squadra object) {
					return "";
				}
			}
		);
		giocatoriCellTable.addColumn(casa, "USERNAME");
        giocatoriCellTable.addColumn(trasferta, "SQUADRA");
    }

    @UiField CellTable<Squadra> giocatoriCellTable;
    
    @UiField TextBox username;
    @UiField TextBox squadra;
    
    @UiField Hyperlink home;
    
    @UiField Button salvaButton;
    @UiField Button creaButton;
    @UiField Button cancellaButton;

    @UiHandler("creaButton")
    void confermaClicked(ClickEvent event) {
    	getEventBus().fireEvent(new NuovaSquadraEvent());
    }
    
    @UiHandler("salvaButton")
    void salvaClicked(ClickEvent event) {
    	getEventBus().fireEvent(new AggiornaSquadraEvent());
    }

    @UiHandler("cancellaButton")
    void cancellaClicked(ClickEvent event) {
    	getEventBus().fireEvent(new CancellaSquadraEvent());
    }

    @UiHandler("home")
    void homeClicked(ClickEvent event) {
    	getEventBus().fireEvent(new HomeEvent());
    }
    
    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public void setName(String name) {
        throw new UnsupportedOperationException("setName not supported yet in "+this.getClass().getName());
    }

    @Override
    public String getName() {
        return "Nuova Giornata";
    }
    
	@Override
	public HasData<Squadra> giocatori() {
		return giocatoriCellTable;
	}

	@Override
	public HasValue<String> username() {
		return username;
	}

	@Override
	public HasValue<String> squadra() {
		return squadra;
	}
}
